package com.gzyj.flink.dto.alarm;


import com.gzyj.flink.dto.utils.DateUtils;
import com.gzyj.flink.dto.utils.ToolUtils;
import com.gzyj.flink.gps.GPSPosition;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * 夜间禁行数据
 *
 * @author lhj
 */
@Data
public class ProhibitDrivingData implements Serializable {


    /// <summary>
    /// 不受Reset控制
    /// </summary>
    public Date LocalTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 不受Reset控制
    /// </summary>
    public Date GpsTime = DateUtils.MIN_VALUE();
    ;

    /// <summary>
    /// 判断车辆数据是否开始计算
    /// </summary>
    public boolean IsCalc = false;
    /// <summary>
    /// 满足条件开始时间
    /// </summary>
    public Date ConditionBeginTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 满足条件开始时经度
    /// </summary>
    public double ConditionBeginLongitude = 0;
    /// <summary>
    /// 满足条件开始时纬度
    /// </summary>
    public double ConditionBeginLatitude = 0;
    /// <summary>
    /// 满足条件结束时间
    /// </summary>
    public Date ConditionEndTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 满足条件结束时经度
    /// </summary>
    public double ConditionEndLongitude = 0;
    /// <summary>
    /// 满足条件结束时纬度
    /// </summary>
    public double ConditionEndLatitude = 0;


    /// <summary>
    /// GUID
    /// 单独生成 不受Reset控制
    /// </summary>
    public String SingleGuid = UUID.randomUUID().toString();
    /// <summary>
    /// 是否正在报警
    /// </summary>
    public boolean IsAlarm = false;
    /// <summary>
    /// 报警开始时间
    /// </summary>
    public Date AlarmBeginTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 报警开始时经度
    /// </summary>
    public double AlarmBeginLongitude = 0;
    /// <summary>
    /// 报警开始时纬度
    /// </summary>
    public double AlarmBeginLatitude = 0;
    /// <summary>
    /// 报警结束时间
    /// </summary>
    public Date AlarmEndTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 报警结束时经度
    /// </summary>
    public double AlarmEndLongitude = 0;
    /// <summary>
    /// 报警结束时纬度
    /// </summary>
    public double AlarmEndLatitude = 0;
    /// <summary>
    /// 报警持续过程中的最大速度
    /// </summary>
    public int AlarmMaxSpeed = 0;
    /// <summary>
    /// 报警持续里程
    /// </summary>
    public double AlarmDistance = 0;
    /// <summary>
    /// 报警等级
    /// </summary>
    public int AlarmLevel = 1;
    /// <summary>
    /// 最后发送报警短信得时间
    /// </summary>
    public Date AlarmContentSendTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 发送报警短信得次数
    /// </summary>
    public int AlarmContentCount = 0;

    /// <summary>
    /// 是否已发过提醒信息，夜间禁行每日触发一次提醒，提醒次数和内容读取前端配置
    /// 不受ResetData重置
    /// </summary>
    public boolean IsRemindContent = false;
    /// <summary>
    /// 发送过第一次提醒得时间
    /// 不受ResetData重置
    /// </summary>
    public Date RemindContentDate = DateUtils.MIN_VALUE();
    /// <summary>
    /// 最后发送提醒得时间
    /// 不受ResetData重置
    /// </summary>
    public Date RemindContentSendTime = DateUtils.MIN_VALUE();
    /// <summary>
    /// 发送过提醒得次数
    /// 不受ResetData重置
    /// </summary>
    public int RemindContentCount = 0;

    /// <summary>
    /// 1默认规则 2=5级规则 3=新疆出租车 4=新疆营运车
    /// </summary>
    public int AlarmSetting;

    /// <summary>
    /// 生成GUID
    /// </summary>
    public void GenerateSingleGuid() {
        this.SingleGuid = UUID.randomUUID().toString();
    }

    /// <summary>
    /// 结束报警
    /// </summary>
    public void StopAlarm(GPSPosition gps) {
        this.IsAlarm = false;
        double lon = this.AlarmEndLongitude;
        double lat = this.AlarmEndLatitude;
        this.AlarmEndTime = gps.getGPSTimeExt();
        this.AlarmEndLongitude = gps.getLngExt();
        this.AlarmEndLatitude = gps.getLatExt();
        this.AlarmMaxSpeed = Math.max(this.AlarmMaxSpeed, gps.getSpeedExt());
        this.AlarmDistance = this.AlarmDistance + ToolUtils.GetDistance(lon, lat, gps.getLngExt(), gps.getLatExt());
    }


    /// <summary>
    /// 开始报警
    /// </summary>
    public void StartAlarm(GPSPosition gps) {
        this.IsAlarm = true;
        this.AlarmBeginTime = gps.getGPSTimeExt();
        this.AlarmBeginLongitude = gps.getLngExt();
        this.AlarmBeginLatitude = gps.getLatExt();
        this.AlarmEndTime = gps.getGPSTimeExt();
        this.AlarmEndLongitude = gps.getLngExt();
        this.AlarmEndLatitude = gps.getLatExt();
        this.AlarmMaxSpeed = gps.getSpeedExt();
        this.AlarmDistance = 0;
        //this.AlarmLevel = 1;
    }

    /// <summary>
    /// 记录报警过程中的数据
    /// </summary>
    public void WriteAlarm(GPSPosition gps) {
        double lon = this.AlarmEndLongitude;
        double lat = this.AlarmEndLatitude;
        this.AlarmEndTime = gps.getGPSTimeExt();
        this.AlarmEndLongitude = gps.getLngExt();
        this.AlarmEndLatitude = gps.getLatExt();
        this.AlarmMaxSpeed = Math.max(this.AlarmMaxSpeed, gps.getSpeedExt());
        this.AlarmDistance = this.AlarmDistance + ToolUtils.GetDistance(lon, lat, gps.getLngExt(), gps.getLatExt());
    }

    /// <summary>
    /// 计算计算时间差，返回以秒为单位的结果，如2323.5秒
    /// </summary>
    public double CalcConditionTimeSpan() {
        long ts = this.ConditionEndTime.getTime() - this.ConditionBeginTime.getTime();
        return ts / 1000;

    }

    /// <summary>
    /// 记录计算过程中的数据
    /// </summary>
    public void WriteCalc(GPSPosition gps) {
        this.ConditionEndTime = gps.getGPSTimeExt();
        this.ConditionEndLongitude = gps.getLngExt();
        this.ConditionEndLatitude = gps.getLatExt();
    }


    /// <summary>
    /// 开始计算
    /// </summary>
    public void StartCalc(GPSPosition gps) {
        this.IsCalc = true;
        this.ConditionBeginTime = gps.getGPSTimeExt();
        this.ConditionBeginLongitude = gps.getLngExt();
        this.ConditionBeginLatitude = gps.getLatExt();
        this.ConditionEndTime = gps.getGPSTimeExt();
        this.ConditionEndLongitude = gps.getLngExt();
        this.ConditionEndLatitude = gps.getLatExt();
    }


    /// <summary>
    /// 重置计算数据
    /// </summary>
    public void ResetData() {
        this.IsCalc = false;
        this.ConditionBeginTime = DateUtils.MIN_VALUE();
        this.ConditionBeginLongitude = 0;
        this.ConditionBeginLatitude = 0;
        this.ConditionEndTime = DateUtils.MIN_VALUE();
        this.ConditionEndLongitude = 0;
        this.ConditionEndLatitude = 0;

        this.IsAlarm = false;
        this.AlarmBeginTime = DateUtils.MIN_VALUE();
        this.AlarmBeginLongitude = 0;
        this.AlarmBeginLatitude = 0;
        this.AlarmEndTime = DateUtils.MIN_VALUE();
        this.AlarmEndLongitude = 0;
        this.AlarmEndLatitude = 0;
        this.AlarmMaxSpeed = 0;
        this.AlarmDistance = 0;
        this.AlarmLevel = 1;
        this.AlarmContentSendTime = DateUtils.MIN_VALUE();
        this.AlarmContentCount = 0;

        this.AlarmSetting = 0;
    }

    public void RemindReset() {
        if (!DateUtils.parseDay(this.RemindContentDate).equalsIgnoreCase(DateUtils.getTotay())) {
            this.IsRemindContent = false;
            this.RemindContentDate = DateUtils.getToday();
            this.RemindContentCount = 0;
            this.RemindContentSendTime = DateUtils.MIN_VALUE();
        }
    }

}
